package com.example.backend.repository;

import com.example.backend.domain.entity.SystemSetting;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

/**
 * 系统设置Repository
 */
public interface SystemSettingRepository extends JpaRepository<SystemSetting, Long> {
    
    // 根据分类查找设置
    List<SystemSetting> findByCategory(String category);
    
    // 根据分类和键查找设置
    Optional<SystemSetting> findByCategoryAndKey(String category, String key);
    
    // 检查设置是否存在
    boolean existsByCategoryAndKey(String category, String key);
    
    // 根据分类分页查询
    Page<SystemSetting> findByCategory(String category, Pageable pageable);
    
    // 搜索设置
    @Query("SELECT s FROM SystemSetting s WHERE " +
           "(:category IS NULL OR s.category = :category) AND " +
           "(:keyword IS NULL OR s.key LIKE %:keyword% OR s.description LIKE %:keyword%)")
    Page<SystemSetting> searchSettings(@Param("category") String category, 
                                      @Param("keyword") String keyword, 
                                      Pageable pageable);
    
    // 获取所有分类
    @Query("SELECT DISTINCT s.category FROM SystemSetting s ORDER BY s.category")
    List<String> findAllCategories();
    
    // 统计设置数量
    long countByCategory(String category);
}
